package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.saas.domain.dao.CustomerStatementDao;
import com.xbongbong.saas.domain.entity.CustomerStatementEntity;
import com.xbongbong.saas.model.CustomerStatementModel;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CustomerStatementModelImpl implements CustomerStatementModel {

    @Resource
    private CustomerStatementDao customerStatementDao;

    @Resource
    private PaasEsModel paasEsModel;

    @Override
    public Integer insert(CustomerStatementEntity entity) throws XbbException {
        Integer success = customerStatementDao.insert(entity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(entity, IndexTypeEnum.IDX_SAAS_CUSTOMER_STATEMENT);
        paasEsModel.insert(insertDTO);
        return success;
    }

    @Override
    public void insertBatch(List<CustomerStatementEntity> entityList) throws XbbException{
        insertBatch(true, entityList);
    }

    @Override
    public void insertBatch(boolean dealWithEs, List<CustomerStatementEntity> entityList) throws XbbException{
        customerStatementDao.insertBatch(entityList);
        if (dealWithEs) {
            //ES数据插入
            List<InsertDTO> insertList = new ArrayList<>();
            for (CustomerStatementEntity item : entityList) {
                InsertDTO insertDTO = new InsertDTO();
                insertDTO.setEsId(item.getCorpid() + "_" + item.getId());
                item.setDataId(item.getId());
                insertDTO.setSource(BeanUtil.convertBean2Map(item,true, true));
                insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CUSTOMER_STATEMENT);
                insertList.add(insertDTO);
            }
            paasEsModel.insertBatch(insertList);
        }
    }

    @Override
    public Integer update(CustomerStatementEntity entity) throws XbbException {
        return null;
    }

    @Override
    public List<CustomerStatementEntity> findEntitys(Map<String, Object> param) {
        return customerStatementDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return customerStatementDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 批量删除对账单信息
     *
     * @param shouldDeleteIdList 对账单中需要删除的对账单数据id
     * @param corpid 公司id
     * @param del 删除标记
     * @return
     */
    @Override
    public Integer deleteBatch(List<Long> shouldDeleteIdList, String corpid, Integer del) throws XbbException {
        Integer integer = customerStatementDao.deleteBatch(shouldDeleteIdList, corpid);
        //ES根据id批量设置del=1
        paasEsModel.deleteBatch(shouldDeleteIdList, corpid, DelEnum.DELETE.getDel(), IndexTypeEnum.IDX_SAAS_CUSTOMER_STATEMENT);
        return integer;
    }

    @Override
    public Integer updateBatch(List<CustomerStatementEntity> list, String corpid) throws XbbException {
        if(CollectionsUtil.isEmpty(list)){
            return 0;
        }
        Integer integer = customerStatementDao.updateBatch(list, corpid);
        updateBatchEs(list);
        return integer;
    }

    @Override
    public Integer updateBatchAbnormal(List<CustomerStatementEntity> list, String corpid) throws XbbException {
        if(CollectionsUtil.isEmpty(list)){
            return 0;
        }
        Integer integer = customerStatementDao.updateBatchAbnormal(list, corpid);
        updateBatchEs(list);
        return integer;
    }

    @Override
    public Integer updateBatchAbnormalForDB(List<CustomerStatementEntity> list, String corpid,boolean isUpdateEs) throws XbbException {
        if(CollectionsUtil.isEmpty(list)){
            return 0;
        }
        Integer integer = customerStatementDao.updateBatchAbnormal(list, corpid);
        if (isUpdateEs){
            updateBatchEs(list);
        }
        return integer;
    }


    @Override
    public void updateBatchEs(List<CustomerStatementEntity> list) throws XbbException {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (CustomerStatementEntity entity : list) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(entity.getCorpid()+"_"+entity.getId());
            entity.setDataId(entity.getId());
            insertDTO.setSource(BeanUtil.convertBean2Map(entity, true, true));
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CUSTOMER_STATEMENT);
            insertDTOList.add(insertDTO);
        }
        //默认不抛出
        paasEsModel.updateBatch(insertDTOList, WriteRequest.RefreshPolicy.WAIT_UNTIL, false);
    }

    @Override
    public CustomerStatementEntity getByKey(Long key, String corpid) {
        return customerStatementDao.getByKey(key, corpid);
    }

    @Override
    public Long getMaxId(Map<String, Object> param) {
        return customerStatementDao.getMaxId(param);
    }
}
